const {saveViewService,getOnePagePvService,getAuthorPvService,getDateAuthorPvService,getDateArticlePvService,getPvSumSevice,articleCountService,getWordCloudService} = require('../service/pv.servcie')
const {saveLikedService,getOneArticleLikedService,getAllArticleLikedService} = require('../service/LikedService.service.js') 
const {saveCollectService,getArticleCollectService,cancelArticleCollectService,getCollectListService,} = require('../service/collect.service.js')
class analyseController {
    
  /**
   * 
   *          PV
   * 
   */

    //   pv埋点 参数：id :文章id
    async savePageView(ctx,next){
        const {id,user_id} =  ctx.request.body
        try{
            const res = await saveViewService({id,user_id})
            ctx.body ={
                code:1000,
                res
            }
        }catch(err){

        }  
    } 
     
    // 单篇文章pv  参数： id  :文章id
    async getOnePagePv(ctx,next){
        const {id} =  ctx.request.query
        try{
            const res = await getOnePagePvService({id})
            ctx.body ={
                code:1000,
                data:res
            }
        }catch(err){

        }  
    }

    //  作者id--也就是user_id  'coun't:16
     async getAuthorPv(ctx,next){
        const {user_id} =  ctx.request.query
        try{
            const res = await getAuthorPvService({user_id})
            ctx.body ={
                code:1000,
                data:res
            }
        }catch(err){

        }  
    }
    //  作者近 7 15 30天访问量  https://www.cnblogs.com/sleepingspring/articles/12000069.html
     
    async getDateAuthorPv(ctx,next){
        const {user_id,dateFrom,dateTo} =  ctx.request.query
        try{
      
            const res = await getDateAuthorPvService({user_id,dateFrom,dateTo})
            console.log(res)
            ctx.body ={
                code:1000,
                data:res
            }
        }catch(err){

        }  
    }
    
    //  此文章近 7 15 30天访问量
     async getDateArticlePv(ctx,next){
        const {id,dateFrom,dateTo} =  ctx.request.query
        try{
            const res = await getDateArticlePvService({id,dateFrom,dateTo})
            ctx.body ={
                code:1000,
                data:res
            }
        }catch(err){
            console.log(err)
        }  
    }
    async getPvSum(ctx,next){
        try{
            const res = await getPvSumSevice()
            ctx.body ={
                code:1000,
                data:res
            }
        }catch(err){
            console.log(err)
        }  
    }


    /***
     * 
     *    Like
     * 
     */
     //  点赞埋点
     async saveLiked(ctx,next){
        const { article_id,author,authoruser_id,operateuser,title,operateuser_id} =  ctx.request.body
        try{
            const res = await saveLikedService({ article_id,author,authoruser_id,operateuser,title,operateuser_id})
            ctx.body ={
                code:1000,
                data:res
            }
        }catch(err){
            console.log(err)
        }  
    }
    // 作者文章的点赞量
    async getAllArticleLiked(ctx,next){
        const { user_id } = ctx.request.query
        try{
            const res = await getAllArticleLikedService({user_id})
            ctx.body ={
                code:1000,
                res
            }
        }catch(err){
            console.log(err)
        }
    }
        // 单篇文章点赞量
        async getOneArticleLiked(ctx,next){
            const { article_id,operateuser_id } =  ctx.request.query
            try{
                const res = await getOneArticleLikedService({ article_id,operateuser_id })
                console.log(res,'🌶')
                ctx.body ={
                    code:1000,
                    data:res
                }
            }catch(err){
                console.log(err)
            }  
        }
      /**
       *   收藏
       * 
       */
      async saveCollect(ctx,next){
            const { article_id,author,authoruser_id,operateuser,title,operateuser_id} =  ctx.request.body
            try{
                const res = await saveCollectService({article_id,author,authoruser_id,operateuser,title,operateuser_id})
                console.log(res,'🌶')
                ctx.body ={
                    code:1000,
                    data:res
                }
            }catch(err){
                console.log(err)
            }
      }

    //   获得单篇文章的
    async getArticleCollect(ctx,next){
        const { article_id,operateuser_id} =  ctx.request.query
        try{
            const res = await getArticleCollectService({article_id,operateuser_id})
            ctx.body ={
                code:1000,
                data:res
            }
        }catch(err){
            console.log(err)
        }
    }
    // 取消文章收藏  
    async cancelArticleCollect(ctx,next){
        const { article_id,operateuser_id} =  ctx.request.body
        try{
            const res = await cancelArticleCollectService({article_id,operateuser_id})
            ctx.body ={
                code:1000,
                data:res
            }
        }catch(err){
            console.log(err)
        }
    }

    //  articleCount
    async articleCount(ctx,next){
        try{
            const res = await articleCountService()
            ctx.body ={
                code:1000,
                data:res
            }
        }catch(err){
            console.log(err)
        }
    }
    async getCollectList(ctx,next){
        const { operateuser_id } = ctx.request.query
        try{
            const res = await getCollectListService({operateuser_id})
            ctx.body ={
                code:1000,
                data:res
            }
        }catch(err){
            console.log(err)
        }
    }
    async getWordCloud(ctx,next){
        try{
            const res = await getWordCloudService()
            ctx.body ={
                code:1000,
                data:res
            }
        }catch(err){
            console.log(err)
        }
    }
    
   
}
// 导出实例化的对象
module.exports = new analyseController()

















// function send(params) {
//     return new Promise((resolve, reject)=>{
//       request(`https://restapi.amap.com/v3/ip?ip=${ip_param}&output=json&key=${gaode_key}`, function (error, response, body) {
//        })
//   })
// }
//   let res = await co(function name() {
//       let req_res = send()
//       return req_res
//   })
//   console.log(res)
//   ctx.body = res
// }


// request(`https://restapi.amap.com/v3/ip?ip=${ip_param}&output=json&key=${gaode_key}`, function (error, response, body) {
          

//     let city = JSON.parse(body).adcode
//     data = JSON.parse(body).adcode
//     console.log(data)
//     ctx.body = await data
//     console.log(data.adcode) // }
//    request(`https://restapi.amap.com/v3/weather/weatherInfo?city=${city}&key=${gaode_key}`, function (err, response, body){
//         data = body
//         console.log(data)
//         ctx.body = 'aaa'
//       })

// })